<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <!-- 页面meta -->
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>体检机构地址管理</title>
    <meta name="description" content="体检机构地址管理">
    <meta name="keywords" content="体检机构地址管理">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }

        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

    </style>
    <script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=bPVmTO9MO4NYCGMLcm9CHozEGoH2cTRF"></script>
</head>

<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>体检机构管理<small>体检机构地址管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>体检机构管理</el-breadcrumb-item>
            <el-breadcrumb-item>体检机构地址管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="体检机构名/体检机构" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="initmap()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="体检机构名" align="center"></el-table-column>
                <el-table-column prop="telephone" label="联系电话" align="center"></el-table-column>
                <el-table-column prop="coordinate" label="经纬度" align="center"></el-table-column>
                <el-table-column prop="address" label="机构位置" align="center"></el-table-column>
                <el-table-column prop="img" label="图片" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="updateAddress(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="deleteAddress(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增体检机构" :visible.sync="dialogFormVisible" @opened="createMap">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="体检机构名" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="联系电话" prop="telephone">
                                    <el-input v-model="formData.telephone"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="上传体检机构地址图片">
                                    <el-upload
                                            class="avatar-uploader"
                                            action="/address/upload.do"
                                            :auto-upload="autoUpload"
                                            name="imgFile"
                                            :show-file-list="false"
                                            :on-success="handleAvatarSuccess"
                                            :before-upload="beforeAvatarUpload">
                                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                    </el-upload>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="经纬度">
                                    <el-input v-model="formData.coordinate"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="详细地址" prop="address">
                                    <el-input v-model="formData.address" @blur="addautolockaddress()"></el-input>
                                </el-form-item>
                            </el-col>
                            </el-input>
                            <el-button type="primary" round icon="el-icon-location-outline" @click="handleAddress()">
                                设置定位
                            </el-button>
                        </el-row>
                        <el-row aligin="center">
                            <!--百度地图容器-->
                            <div style="width:100%;height:350px;border:#ccc solid 1px;font-size:12px" id="map1"></div>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="addAddress()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="edit-form">
                <el-dialog title="编辑体检机构" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="体检机构名" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="联系电话" prop="telephone">
                                    <el-input v-model="formData.telephone"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="上传体检机构地址图片">
                                    <el-upload
                                            class="avatar-uploader"
                                            action="/address/upload.do"
                                            :auto-upload="autoUpload"
                                            name="imgFile"
                                            :show-file-list="false"
                                            :on-success="handleAvatarSuccess"
                                            :before-upload="beforeAvatarUpload">
                                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                    </el-upload>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="经纬度">
                                    <el-input v-model="formData.coordinate"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="详细地址">
                                    <el-input v-model="formData.address" @blur="editautolockaddress()"></el-input>
                                </el-form-item>
                            </el-col>
                            </el-input>
                            <el-button type="primary" round icon="el-icon-location-outline" @click="handleAddress()">
                                设置定位
                            </el-button>
                        </el-row>
                        <el-row aligin="center">
                            <!--百度地图容器-->
                            <div style="width:100%;height:350px;border:#ccc solid 1px;font-size:12px" id="map2"></div>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="editAddress()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            autoUpload: true,//自动上传
            imageUrl: null,//模型数据，用于上传图片完成后图片预览
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            api: "http://qzmlh6zdc.hd-bkt.clouddn.com/",
            rules: {//校验规则
                name: [{required: true, message: '体检机构名为必填项', trigger: 'blur'}],
                telephone: [{required: true, message: '手机号为必填项', trigger: 'blur'}],
                address: [{required: true, message: '地址为必填项', trigger: 'blur'}]
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },
        methods: {
            //文件上传成功后的钩子，response为服务端返回的值，file为当前上传的文件封装成的js对象
            handleAvatarSuccess(response, file) {
                //为模型数据imageUrl赋值，用于页面图片预览
                this.imageUrl = 'http://qzmlh6zdc.hd-bkt.clouddn.com/' + response.data;
                this.$message({
                    type: response.flag ? 'success' : 'error',
                    message: response.message
                });
                //设置模型数据（图片名称），后续提交ajax请求时会提交到后台最终保存到数据库
                this.formData.img = response.data;
            },
            //上传图片之前执行
            beforeAvatarUpload(file) {
                const isIMAGE = file.type === 'image/jpeg'||'image/gif'||'image/png';
                const isLt5M = file.size / 1024 / 1024 < 5;
                if (!isIMAGE) {
                    this.$message.error('上传套餐图片只能是 JPG 格式!');
                }
                if (!isLt5M) {
                    this.$message.error('上传套餐图片大小不能超过 5MB!');
                }
                return isIMAGE && isLt5M;
            },
            // 弹出编辑窗口回写
            updateAddress(row) {
                this.dialogFormVisible4Edit = true;
                this.resetForm();
                axios.get("/address/findById.do?id=" + row.id).then((res) => {
                    if (res.data.flag) {
                        this.formData = res.data.data;
                        this.imageUrl = 'http://qzmlh6zdc.hd-bkt.clouddn.com/' + res.data.data.img;
                        var c = res.data.data.coordinate.split(",");
                        createMap2(c[0], c[1]);//创建地图
                        setMap2Event();//设置地图事件
                        addMap2Control();//向地图添加控件
                    } else {
                        this.$message.error(res.data.message);
                    }
                });
            },
            // 修改地址
            editAddress() {
                this.$refs['dataEditForm'].validate((valide) => {
                    if (valide) {
                        if (checkTelephone(this.formData.telephone)) {
                            axios.post("/address/handleEdit.do", this.formData).then((res) => {
                                if (res.data.flag) {
                                    this.$message.success(res.data.message);
                                    this.dialogFormVisible4Edit = false;
                                    this.findPage();
                                } else {
                                    this.$message.error(res.data.message);
                                }
                            });
                        } else {
                            this.$message.error("手机号输入有误!");
                        }
                    } else {
                        this.$message.error("数据格式有误!");
                    }
                });
            },
            //添加
            addAddress() {
                this.$refs['dataAddForm'].validate((valide) => {
                    if (valide) {
                        if (checkTelephone(this.formData.telephone)) {
                            axios.post("/address/add.do", this.formData).then((res) => {
                                if (res.data.flag) {
                                    this.$message.success(res.data.message);
                                    this.dialogFormVisible = false;
                                    this.findPage();
                                } else {
                                    this.$message.error(res.data.message);
                                }
                            });
                        } else {
                            this.$message.error("手机号输入有误!");
                        }
                    } else {
                        this.$message.error("数据格式有误!");
                    }
                });
            },
            //分页查询
            findPage() {
                var param = {
                    currentPage: this.pagination.currentPage,//页码
                    pageSize: this.pagination.pageSize,//每页显示的记录数
                    queryString: this.pagination.queryString//查询条件
                };
                axios.post("/address/findPage.do", param).then((response) => {
                    this.dataList = response.data.rows;
                    this.pagination.total = response.data.total;
                });
            },
            // 重置表单
            resetForm() {
                this.formData = {};
                this.imageUrl = null;
                this.$nextTick(() => {
                    this.$refs['dataAddForm'].clearValidate();
                });
            },
            // 初始化地图
            initmap() {
                this.dialogFormVisible = true;
                this.resetForm();
                this.$nextTick(() => {
                    createMap1();//创建地图
                    setMap1Event();//设置地图事件
                    addMap1Control();//向地图添加控件
                });
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            // 删除
            deleteAddress(row) {
                this.$confirm("确定要删除这条数据吗!", "提示", {
                    type: "warning"
                }).then(() => {
                    //用户点击确定按钮，发送ajax请求，将地址的ID提交到Controller进行处理
                    axios.get("/address/delete.do?id=" + row.id).then((res) => {
                        if (res.data.flag) {
                            //执行成功
                            //弹出成功提示信息
                            this.$message({
                                type: 'success',
                                message: res.data.message
                            });
                            //重新进行分页查询
                            this.findPage();
                        } else {
                            //执行失败
                            this.$message.error(res.data.message);
                        }
                    }).catch((r) => {
                        this.showMessage(r);
                    });
                }).catch(() => {
                    this.$message.info("操作已取消");
                });
            },
            //权限不足提示
            showMessage(r) {

            },
            handleAddress() {
                this.formData = {
                    id: this.formData.id,
                    name: this.formData.name,
                    telephone: this.formData.telephone,
                    img: this.formData.img,
                    coordinate: bdcoordinate,
                    address: bdaddress
                };
            },

            // 根据地址自动锁定在地图的位置
            addautolockaddress() {
                var address = this.formData.address;
                if (address != null) {
                    // 根据地址名定位
                    locationbycityname(address);
                }
            },
            editautolockaddress() {
                var address = this.formData.address;
                if (address != null) {
                    editLocationbycityname(address);
                }
            }
        }
    });
    /*地图相关函数*/
    var bdcoordinate = '';
    var bdaddress = '';
    //创建和初始化地图函数
    var map1;

    // 创建地图
    function createMap1() {
        // 百度地图API功能
        map1 = new BMap.Map("map1");
        var point = new BMap.Point(117.28269909,31.86694226);
        map1.centerAndZoom(point,10);
    }

    // 鼠标点击获取地址
    function setMap1Event() {
        map1.enableScrollWheelZoom(); //开启鼠标滚轮缩放
        map1.enableKeyboard();        //启用键盘操作
        map1.enableDragging();        //启用地图拖拽
        map1.enableDoubleClickZoom(); //启用双击放大
        map1.addEventListener('click', function (e) {
            map1.clearOverlays(); //清除地图上所有覆盖物

            // 创建标注
            var pt = e.point;
            var point = new BMap.Point(pt.lng, pt.lat);     //设置经纬度
            var marker = new BMap.Marker(point);
            map1.addOverlay(marker);     // 将标注添加到地图中

            // 创建地理编码实例
            var geocoder = new BMap.Geocoder();
            // 根据坐标得到地址描述
            geocoder.getLocation(e.point, function (rs) {
                bdcoordinate = pt.lng + "," + pt.lat;   //经度+纬度 打印拖动结束坐标
                bdaddress = rs.address;                 //显示获取的地址
            });
        });
    }
    // 修改地图
    var map2;
    var marker;

    function createMap2(lng, lat) {
        map2 = new BMap.Map("map2");
        var point;
        if (lng != null && lat != null) {
            point = new BMap.Point(lng, lat);
            marker = new BMap.Marker(point);        // 创建标注
            map2.addOverlay(marker);
        } else {
            point = new BMap.Point(117.28269909,31.86694226);
        }
        map2.centerAndZoom(point, 10);
    }

    function setMap2Event() {
        map2.enableScrollWheelZoom();
        map2.enableKeyboard();
        map2.enableDragging();
        map2.enableDoubleClickZoom();
        map2.addEventListener('click', function (e) {
            var pt = e.point;
            map2.removeOverlay(marker);
            var point = new BMap.Point(pt.lng, pt.lat);
            marker = new BMap.Marker(point);        // 创建标注
            map2.addOverlay(marker);
            var geocoder = new BMap.Geocoder();
            geocoder.getLocation(e.point, function (rs) {
                bdcoordinate = pt.lng + "," + pt.lat;
                bdaddress = rs.address;
            });
        });
    }
    // 根据地址定位地图位置
    function editLocationbycityname(value) {
        var local, point, marker = null;
        local = new BMap.LocalSearch(map2, { //智能搜索
            onSearchComplete: fn
        });

        local.search(value);

        function fn() {
            //如果搜索的有结果
            if (local.getResults() != undefined) {
                map2.clearOverlays(); //清除地图上所有覆盖物
                if (local.getResults().getPoi(0)) {
                    point = local.getResults().getPoi(0).point; //获取第一个智能搜索的结果

                    //map.centerAndZoom方法创建地图，第一个参数可以是根据之前创建好的一个点为中心，创建出地图，也可以根据城市地区的中文名称创建地图,第二个参数是地图缩放级别，最大为19，最小为0
                    map2.centerAndZoom(point, 18);
                    marker = new BMap.Marker(point); // 创建标注
                    map2.addOverlay(marker); // 将标注添加到地图中
                    marker.enableDragging(); // 可拖拽
                    bdcoordinate = point.lng + "," + point.lat;
                    // 创建地理编码实例, 并配置参数获取乡镇级数据
                    var myGeo = new BMap.Geocoder({extensions_town: true});
                    // 根据坐标得到地址描述
                    myGeo.getLocation(new BMap.Point(point.lng, point.lat), function (result) {
                        if (result) {
                            bdaddress = result.address;
                        }
                    });
                } else {
                    this.$message.error("未匹配到地点!可拖动地图上的红色图标到精确位置");
                }
            } else {
                this.$message.error("未找到搜索结果");
            }
        }
    }

    //向地图添加控件
    function addMap2Control() {
        var scaleControl = new BMap.ScaleControl({anchor: BMAP_ANCHOR_BOTTOM_LEFT});
        scaleControl.setUnit(BMAP_UNIT_IMPERIAL);
        map2.addControl(scaleControl);
        var navControl = new BMap.NavigationControl({
            anchor: BMAP_ANCHOR_TOP_LEFT,
            type: BMAP_NAVIGATION_CONTROL_LARGE
        });
        map2.addControl(navControl);
        var overviewControl = new BMap.OverviewMapControl({anchor: BMAP_ANCHOR_BOTTOM_RIGHT, isOpen: true});
        map2.addControl(overviewControl);
    }

    //加载地图事件
    function addClickHandler(target, window) {
        target.addEventListener("click", function () {
            target.openInfoWindow(window);   //开启信息窗口
        });
    }

    //向地图添加控件
    function addMap1Control() {
        //ScaleControl 比例尺控件，默认位于地图左下方，显示地图的比例关系。
        var scaleControl = new BMap.ScaleControl({anchor: BMAP_ANCHOR_BOTTOM_LEFT});
        scaleControl.setUnit(BMAP_UNIT_IMPERIAL);  //英制单位
        map1.addControl(scaleControl);
        // 添加带有定位的导航控件
        //NavigationControl 地图平移缩放控件，PC端默认位于地图左上方，它包含控制地图的平移和缩放的功能。
        var navControl = new BMap.NavigationControl({
            anchor: BMAP_ANCHOR_TOP_LEFT,          //靠左上角位置
            type: BMAP_NAVIGATION_CONTROL_LARGE    //LARGE类型
        });
        map1.addControl(navControl);

        //OverviewMapControl 缩略地图控件，默认位于地图右下方，是一个可折叠的缩略地图。
        var overviewControl = new BMap.OverviewMapControl({anchor: BMAP_ANCHOR_BOTTOM_RIGHT, isOpen: true});
        map1.addControl(overviewControl);

    }
    // 根据地址定位地图位置
    function locationbycityname(value) {
        var local, point, marker = null;
        local = new BMap.LocalSearch(map1, { //智能搜索
            onSearchComplete: fn
        });

        local.search(value);

        function fn() {
            //如果搜索的有结果
            if (local.getResults() != undefined) {
                map1.clearOverlays(); //清除地图上所有覆盖物
                if (local.getResults().getPoi(0)) {
                    point = local.getResults().getPoi(0).point; //获取第一个智能搜索的结果

                    //map.centerAndZoom方法创建地图，第一个参数可以是根据之前创建好的一个点为中心，创建出地图，也可以根据城市地区的中文名称创建地图,第二个参数是地图缩放级别，最大为19，最小为0
                    map1.centerAndZoom(point, 18);
                    marker = new BMap.Marker(point); // 创建标注
                    map1.addOverlay(marker); // 将标注添加到地图中
                    marker.enableDragging(); // 可拖拽
                    bdcoordinate = point.lng + "," + point.lat;
                    // 创建地理编码实例, 并配置参数获取乡镇级数据
                    var myGeo = new BMap.Geocoder({extensions_town: true});
                    // 根据坐标得到地址描述
                    myGeo.getLocation(new BMap.Point(point.lng, point.lat), function (result) {
                        if (result) {
                            bdaddress = result.address;
                        }
                    });
                } else {
                    this.$message.error("未匹配到地点!可拖动地图上的红色图标到精确位置");
                }
            } else {
                this.$message.error("未找到搜索结果");
            }
        }
    }
    // 手机号校验
    function checkTelephone(telephone) {
        var reg = /^[1][3,4,6,5,7,8][0-9]{9}$/;
        if (!reg.test(telephone)) {
            return false;
        } else {
            return true;
        }
    }
</script>

</html>
